<?php

namespace Modules\Admin\Http\Controllers\Admin;

use App\Http\Controllers\Admin\ApiController;
use Illuminate\Contracts\Support\Renderable;
use Illuminate\Http\Request;
use Modules\Admin\Entities\Permission;
use Modules\CourseEvaluation\Services\Process\CourseEvaluationProcessInfoService;

class MenuController extends ApiController
{
    /**
     * Display a listing of the resource.
     * @return Renderable
     */
    public function index(Request $request)
    {
        // $menus = [
        //     [
        //         "name" => "Dashboard",
        //         "title" => "控制台",
        //         "permission_name" => "kong zhi tai",
        //         "children" => [
        //             [
        //                 "name" => "Index",
        //                 "title" => "控制台", //原：控制台
        //                 "permission_name" => "kong zhi tai", // 原：kong zhi tai
        //             ],
        //         ]
        //     ],
        //     [
        //         "name" => "ActivityLog",
        //         "title" => "操作日志",
        //         "permission_name" => "cao zuo ri zhi",
        //         "children" => [
        //             [
        //                 "name" => "List",
        //                 "title" => "操作日志",
        //                 "permission_name" => "cao zuo ri zhi",
        //             ],
        //         ]
        //     ],
        //     [
        //         "name" => "Permissions",
        //         "title" => "权限管理",
        //         "permission_name" => "quan xian guan li",
        //         "children" => [
        //             [
        //                 "name" => "Admin",
        //                 "title" => "管理员",
        //                 "permission_name" => "quan xian guan li.guan li yuan",
        //             ],
        //             [
        //                 "name" => "Roles",
        //                 "title" => "角色列表",
        //                 "permission_name" => "quan xian guan li.jue se lie biao",
        //             ],
        //             // [
        //             //     "name" => "List",
        //             //     "title" => "权限列表",
        //             //     "permission_name" => "quan xian guan li.quan xian lie biao",
        //             // ],
        //         ]
        //     ],
        // ];

        // $menus = Permission::query()->where('menu', 1)->get()->toTree();

        $menus = Permission::query()->where('menu', 1)->orderBy('sort')->get();
        $menus = $menus->map(function ($menu) {
            $name = $menu->name;
            $menu->offsetSet('name', $menu->page_name);
            $menu->offsetSet('permission_name', $name);
            return $menu;
        })->toTree();

        // 移除没有权限的菜单【以后系统 直接从权限表中配置菜单动态获取】
        if (!$this->admin->super_admin) {
            $menus = $this->filterMenu($menus);
        }
        return $this->success($menus);
    }

    /**
     * 说明：过滤掉没有权限的菜单
     *
     * @return
     * @param   type
     * @author  daixinguo (daixinguo57@163.com)
     */
    public function filterMenu($menus)
    {
        foreach ($menus as $key => &$value) {
            if (!$this->admin->hasAllPermissions($value['permission_name'])) {
                unset($menus[$key]);
            }
            if (is_array($menus)) {
                $menus = array_values($menus);
            }
            if (!empty($value['children']) && count($value['children']) > 0) {
                $value['children'] = $this->filterMenu($value['children']);
            }
        }
        unset($value);

        return $menus;
    }
}
